package com.cgw.room;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.os.Environment;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

import com.cgw.room.bean.BatteryState;
import com.cgw.room.bean.MotionState;
import com.cgw.room.bean.RcsData;
import com.cgw.room.util.FileOperator;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.UUID;

public class MainApp extends Application {
    // CGW BACKUP

    public static String ProductName = "chenmoyang";
    public static String IP_1 = "192";
    public static String IP_2 = "168";
    public static String IP_3 = "1";
    public static String IP_4 = "103";
    public static String PORT = "43893";
    public static String IP_1_AVOIDMODE = "192";
    public static String IP_2_AVOIDMODE = "168";
    public static String IP_3_AVOIDMODE = "1";
    public static String IP_4_AVOIDMODE = "105";
    public static String PORT_AVOIDMODE = "43899";
    public static String PORT_RECV = "43897";
    public static String getIP(){return IP_1 + "." + IP_2 + "." + IP_3 + "." + IP_4;}
    public static String getIP_AvoidMode(){return IP_1_AVOIDMODE + "." + IP_2_AVOIDMODE + "." + IP_3_AVOIDMODE + "." + IP_4_AVOIDMODE;}
    public static int HeartBeatInterval = 500;
    public static MotionState CurrentMotionState = new MotionState();
    public static RcsData CurrentRcsData = new RcsData();
    public static BatteryState CurrentBatteryState = new BatteryState();

    public static boolean checkSendIP(){
        String IP = MainApp.getIP();
        if (IP.isEmpty()){
            MainApp.toast("请配置发送目标IP。", 2000);
            return false;
        }

        String PORT = MainApp.PORT;
        if (PORT.isEmpty()){
            MainApp.toast("请配置发送目标PORT。", 2000);
            return false;
        }

        return true;
    }
    public static boolean checkSendIP_AvoidMode(){
        String IP = MainApp.getIP_AvoidMode();
        if (IP.isEmpty()){
            MainApp.toast("请配置发送目标IP。", 2000);
            return false;
        }

        String PORT = MainApp.PORT_AVOIDMODE;
        if (PORT.isEmpty()){
            MainApp.toast("请配置发送目标PORT。", 2000);
            return false;
        }

        return true;
    }


    private static final Handler sHandler = new Handler();
    private static Toast sToast; // 单例Toast,避免重复创建，显示时间过长
    public static MainApp sInstance;
    public static MainApp getInstance() {
        return sInstance;
    }

    public static Activity mActivity = null;
    // PHY6222 UUID

    public static String[] permission = {
            android.Manifest.permission.BLUETOOTH_SCAN,
            android.Manifest.permission.BLUETOOTH_ADVERTISE,
            android.Manifest.permission.BLUETOOTH_CONNECT,
            android.Manifest.permission.ACCESS_COARSE_LOCATION,
            android.Manifest.permission.ACCESS_FINE_LOCATION};

    @SuppressLint("ShowToast")
    @Override
    public void onCreate() {
        super.onCreate();
        sToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
    }
    public static void toast(String txt, int duration) {
        sToast.setText(txt);
        sToast.setDuration(duration);
        sToast.show();
    }
    public static void runUi(Runnable runnable) {
        sHandler.post(runnable);
    }

    public static String getProductDir(){
        return Environment.getExternalStorageDirectory().toString() + "/" + MainApp.ProductName;
    }
    public static Activity getCurrentActivity(){
        return mActivity;
    }
    public static void setCurrentActivity(Activity activity){
        mActivity = activity;
    }
}
